package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class MyImagerDetailFragment extends BaseFragment {
	
	private static MyImagerDetailFragment homeFragmentInstent;
	private static List<String> urls = new ArrayList<String>();
	private static String url;
	private int index;
	private int curIndex;
	@ViewInject(R.id.vp_myImg)
	private ViewPager vp_myImg;
	private PhotoViewAttacher mAttacher;
	private List<ImageView> views;

	private PhotoView mPhotoView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_myimagedetail, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {

		for (int i = 0;i<urls.size();i++){
			if(url!=null){
				if(url.equals(urls.get(i))){
					index=i;
					curIndex = i+1;
				}
			}
		}
		if(url==null){
			index=1;
		}
		vp_myImg.setCurrentItem(index);
		index=curIndex;

		mBaseActivity.setTitle(index + "/" + urls.size());
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);
		views = new ArrayList<ImageView>();
		/*for (int i = 0;i<urls.size();i++){
			PhotoView imageView = new PhotoView(
					mBaseActivity);

			App.bitmapUtils.display(imageView, urls.get(i));
			mAttacher=new PhotoViewAttacher(imageView);
			views.add(imageView);
		}*/
		MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
		vp_myImg.setAdapter(myPagerAdapter);

		vp_myImg.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int i, float v, int i1) {

			}

			@Override
			public void onPageSelected(int i) {
				index = i + 1;
				mBaseActivity.setTitle(index + "/" + urls.size());
			}

			@Override
			public void onPageScrollStateChanged(int i) {

			}
		});


	}
	class MyPagerAdapter extends PagerAdapter{

		private List<ImageView> views;

		/*public MyPagerAdapter(List<ImageView> views){
			this.views= views;
		}*/
		@Override
		public int getCount() {
			return urls.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View)object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = container.inflate(mBaseActivity,
					R.layout.item_photo_view,null);
			mPhotoView = (PhotoView) view.findViewById(R.id.photo);
			//使用ImageLoader加载图片
			mAttacher = new PhotoViewAttacher(mPhotoView);
			App.bitmapUtils.display(mPhotoView, urls.get(position));
			//给图片增加点击事件
			container.addView(view);
			return view;
		}
	}

	public static void setUrls(List<String> urls) {
		MyImagerDetailFragment.urls = urls;
	}
	public static void setUrl(String url) {
		MyImagerDetailFragment.url = url;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static MyImagerDetailFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
